import axios from 'axios';

export interface OriginalQuestion {
  id: number;  // 对应Java Long
  version: string;
  title: string;
  updateTime: string;  // ISO格式日期字符串
  content: string;
  source: string;
  active: boolean;
}

export interface StandardQuestion {
  id: number;
  question: string;
  originalQuestion: {
    id: number;
    version: string;
  };
  updateTime: string;
  active: boolean;
  category: string;
}

export async function searchOriginalQuestions(
  params: {
    title?: string;
    version?: string;
    page?: number;
    size?: number;
  }
): Promise<OriginalQuestion[]> {
  const { title, version, page = 0, size = 10 } = params;
    const response = await axios.get('/api/questions/original', {
      params: {
        title,
        version,
        page,
        size
      }
    });
    return response.data;
}

export async function getStandardByOriginal(
  originalId: number,
  version: string
): Promise<StandardQuestion[]> {
    const response = await axios.get(`/api/questions/standard/by-original/${originalId}/${version}`);
    console.log('API响应:', {
      url: response.config.url,
      status: response.status,
      data: response.data
    });
    return response.data;
}

export async function getStandardQuestionById(
  id: number,
  version: string
): Promise<StandardQuestion> {
  const response = await axios.get(`/api/questions/standard/${id}/${version}`);
  console.log('API响应:', {
    url: response.config.url,
    status: response.status,
    data: response.data
  });
  return response.data;
}

export async function updateStandardQuestion(
  id: number,
  version:string,
  question: string
): Promise<void> {
  const response = await axios.put('/api/questions/standard', {
    id,
    version,
    question
  });
  console.log('API响应:', {
    url: response.config.url,
    status: response.status,
    data: response.data
  });
}

export async function countSearchResults(
  params: { title?: string; version?: string }
): Promise<number> {
  const { title, version } = params;
  const response = await axios.get('/api/questions/original/search-count', {
    params: {
      title,
      version
    }
  });
  return response.data;
}

export async function toggleStandardQuestionStatus(
  id: number,
  version: string
): Promise<void> {
  const response = await axios.put(`/api/questions/standard/switch/${id}/${version}`);
  console.log('API响应:', {
    url: response.config.url,
    status: response.status,
    data: response.data
  });
}

export async function getStandardQuestionsNoAnswer(
  version: string,
  page: number = 0,
  size: number = 10
): Promise<StandardQuestion[]> {
  const response = await axios.get(`/api/questions/standard/no-answer/${version}`, {
    params: { page, size }
  });
  return response.data;
}

export async function getStandardQuestionsActive(
  version: string,
  page: number = 0,
  size: number = 10
): Promise<StandardQuestion[]> {
  const response = await axios.get(`/api/questions/standard/active/${version}`, {
    params: { page, size }
  });
  return response.data;
}

export async function getStandardQuestionsInactive(
  version: string,
  page: number = 0,
  size: number = 10
): Promise<StandardQuestion[]> {
  const response = await axios.get(`/api/questions/standard/inactive/${version}`, {
    params: { page, size }
  });
  return response.data;
}

export async function countStandardQuestionsNoAnswer(
  version: string
): Promise<number> {
  const response = await axios.get(`/api/questions/standard/no-answer/${version}/count`);
  return response.data;
}

export async function countStandardQuestionsActive(
  version: string
): Promise<number> {
  const response = await axios.get(`/api/questions/standard/active/${version}/count`);
  return response.data;
}

export async function countStandardQuestionsInactive(
  version: string
): Promise<number> {
  const response = await axios.get(`/api/questions/standard/inactive/${version}/count`);
  return response.data;
}

export async function createStandardQuestion(
  question: string,
  originalQuestionId: number,
  originalQuestionVersion: string
): Promise<void> {
  const response = await axios.post('/api/questions/standard', {
    question,
    originalQuestionId,
    originalQuestionVersion
  });
  console.log('API响应:', {
    url: response.config.url,
    status: response.status,
    data: response.data
  });
}

// 标签相关接口
export async function addQuestionTag(
  questionId: number,
  version: string,
  tag: string
): Promise<void> {
  const response = await axios.post(`/api/question-tags/${questionId}/${version}`, null, {
    params: { tag }
  });
  console.log('API响应:', {
    url: response.config.url,
    status: response.status,
    data: response.data
  });
}

export async function removeQuestionTag(
  questionId: number,
  version: string,
  tag: string
): Promise<void> {
  const response = await axios.delete(`/api/question-tags/${questionId}/${version}`, {
    params: { tag }
  });
  console.log('API响应:', {
    url: response.config.url,
    status: response.status,
    data: response.data
  });
}

export async function getQuestionsByTags(
  tags: string[]
): Promise<StandardQuestion[]> {
  const response = await axios.get('/api/question-tags/search', {
    params: { tags: tags.join(',') }
  });
  return response.data;
}

export interface QuestionSearchRequest {
  tags?: string[];
  isActive?: boolean;
  hasStandardAnswer?: boolean;
  keyword?: string;
  versionId: string;
  category?: string;
  page?: never; // 确保不会传递page参数到请求体
  size?: never; // 确保不会传递size参数到请求体
}

export interface PageResponse<T> {
  content: T[];
  totalElements: number;
  totalPages: number;
  number: number;
  size: number;
}

export async function searchStandardQuestions(
  request: QuestionSearchRequest,
  page: number = 0,
  size: number = 10
): Promise<PageResponse<StandardQuestion>> {
  const response = await axios.post('/api/questions/standard/search', request, {
    params: { page, size }
  });
  return response.data;
}

export async function getAllTags(): Promise<string[]> {
  const response = await axios.get('/api/question-tags/all');
  return response.data;
}

export async function getTagsByQuestionId(
  questionId: number,
  version: string
): Promise<string[]> {
  const response = await axios.get(`/api/question-tags/${questionId}/${version}`);
  return response.data;
}

export async function importQADataset(
  version: string,
  originalFile: File,
  standardFile: File
): Promise<void> {
  const formData = new FormData()
  formData.append('originalFile', originalFile)
  formData.append('standardFile', standardFile)
  
  const response = await axios.post(`/api/io/input/${version}`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
  return response.data
}

// 分类相关接口
export interface CategoryUpdateDTO {
  questionId: number;
  version: string;
  category: string;
}

export async function getAllCategories(version: string): Promise<string[]> {
  const response = await axios.get(`/api/categories/${version}`);
  return response.data;
}

export async function setQuestionCategory(
  questionId: number,
  version: string,
  category: string
): Promise<void> {
  const response = await axios.post('/api/categories/set', {
    questionId,
    version,
    category
  });
  console.log('API响应:', {
    url: response.config.url,
    status: response.status,
    data: response.data
  });
}